package xml.resource;

import java.io.IOException;
import java.io.Serializable;
import java.util.List;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.restlet.representation.Representation;
import xml.converter.JDOMElementRepresentationConverter;
import xml.jdom.AbstractJDOMElement;

/**
 * Représente les erreurs liées aux ressources
 * @author marya
 */
public class ResourceError extends AbstractJDOMElement implements Serializable {

    private String libelle;

    public ResourceError(String libelle) {
        this.libelle = libelle;
    }

    public String getLibelle() {
        return libelle;
    }

    public void setLibelle(String libelle) {
        this.libelle = libelle;
    }

    public static Representation getRepresentation(String error) throws IOException, JDOMException {
        return (new ResourceError(error)).toXML();
    }

    public static Representation getListRepresentation(List<ResourceError> errors) throws IOException, JDOMException {
        Element racine = new Element("errors");
        if (errors != null) {
            for (ResourceError error : errors) {
                racine.addContent(error.toJDOMElement());
            }
        }
        return (new JDOMElementRepresentationConverter()).convert(racine);
    }

    public Element toJDOMElement() {
        Element racine = new Element("error");
        racine.setText(libelle);
        return racine;
    }
}
